<?php
/**
 * 模块管理
 * @author Administrator
 *
 */
class moduleManage extends CControllerManageBase
{
	public function show()
	{
		//显示列表
		$this->getModule();
		//var_dump($this->tempModuleTree);
		$modelGroupObj = new moduleGroupModel();
        $this->setViewParam('allModuleGroup',$modelGroupObj->selectAll());
        $this->setViewParam('allModule',$this->tempModuleTree);
		$this->setMenu("模块列表");
        $this->render("module/index");
	}
	
	public function addgroup()
	{
		$errorStr="";
		$rule=array(
			"txt_group_name"=>array(
				"formValidator"=>array("tipid"=>"txt_group_name_tip","empty"=>"false","onshow"=>"请输入模块组名","onfocus"=>"请输入模块组名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块组名不能为空")
			)
		);
		
		$this->formValidator->init(array("formID"=>"tbl_base_module_groupForm"));
		$this->formValidator->setRules($rule);
		if($_POST){
			$modelInfo = array();
			$modelInfo["group_name"] = $this->getRequestData("txt_group_name");
			$modelInfo["group_level"] = 0;
			$modelInfo["del_flag"] = 0;
			$modelInfo["create_time"] = date('Y-m-d H:i:s',strtotime ("now"));
			$modelInfo["update_time"] = date('Y-m-d H:i:s',strtotime ("now"));

			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new moduleGroupModel();
				$id = $model->add($modelInfo);
				if($id>0)
				{
					$this->sysMsg("新增模块组成功",0,array(array("text"=>"返回列表","href"=>$this->_manageDomain."moduleManage/show/"),array("text"=>"继续新增","href"=>$this->_manageDomain."moduleManage/addgroup")),true);
				}
			}
		}
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),array("text"=>"模块组新增")));
		$this->render("module/addgroup",array("error"=>$errorStr));
	}
	
	public function editGroup()
	{
		$module_group_id = $this->getRequestData("id","GET");
		$modelInfo = array();
		$errorStr = "";

		$rule=array(
			"txt_group_name"=>array(
				"formValidator"=>array("tipid"=>"txt_group_name_tip","empty"=>"false","onshow"=>"请输入模块组名","onfocus"=>"请输入模块组名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块组名不能为空")
			)
		);
		$this->formValidator->init(array("formID"=>"tbl_base_module_groupForm"));
		$this->formValidator->setRules($rule);

		$model=new moduleGroupModel();
		if($_POST){
			$modelInfo["module_group_id"] = $module_group_id;
			$modelInfo["group_name"] = $this->getRequestData("txt_group_name");
			$modelInfo["update_time"] = date('Y-m-d H:i:s',strtotime ("now"));
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model->updateByPkId($modelInfo);
				header("location:/admin/moduleManage/show");
				exit();
			}
		}
		else{
			$modelInfo = $model->selectById($module_group_id);

			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
		}
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),array("text"=>"模块组修改")));
		$this->render("module/editgroup",array("error"=>$errorStr,"modelInfo"=>$modelInfo));
	}
	
	public function addModule()
	{
		$moduleGroupID = $this->getRequestData("groupid",'GET');
		if(!$moduleGroupID)
		{
			$this->sysMsg("模块组ID不正确",1);
		}
		$errorStr="";
		$rule=array(
			"txt_module_name"=>array(
				"formValidator"=>array("tipid"=>"txt_module_name_tip","empty"=>"false","onshow"=>"请输入模块名","onfocus"=>"请输入模块名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块名不能为空")
			),
			"txt_identity_name"=>array(
				"formValidator"=>array("tipid"=>"txt_identity_name_tip","empty"=>"false","onshow"=>"请输入模块唯一标识","onfocus"=>"请输入模块唯一标识","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块唯一标识不能为空")
			),
			"txt_url"=>array(
				"formValidator"=>array("tipid"=>"txt_url_tip","empty"=>"false","onshow"=>"请输入模块访问路径","onfocus"=>"请输入模块访问路径","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块访问路径不能为空")
			)
		);
		$this->formValidator->init(array("formID"=>"tbl_base_moduleForm"));
		$this->formValidator->setRules($rule);
		if($_POST){
			$modelInfo = array();
			$modelInfo["module_name"] = $this->getRequestData("txt_module_name");//(模块名)
			$modelInfo["identity_name"] = $this->getRequestData("txt_identity_name");//(模块唯一标识)
			$modelInfo["url"] = $this->getRequestData("txt_url");//(模块访问路径)
			$modelInfo["description"] = $this->getRequestData("txt_description");//(模块描述)
			$modelInfo["module_group_id"] = $moduleGroupID;//模块分组ID
			$modelInfo["sort_num"] = $this->getRequestData("txt_sort");//模块排序)
			$modelInfo["create_time"] = date('Y-m-d H:i:s',strtotime("now"));//(创建时间)
			//$modelInfo["version_id"] = $this->getRequestData("txt_version_id");//(版本号)
			$modelInfo["del_flag"] = 0;//(删除标识)
			$modelInfo["module_state"] = $this->getRequestData("moduleState");//模块状态
			$modelInfo["update_time"] = date('Y-m-d H:i:s',strtotime ("now"));//修改日期
			$modelInfo["is_show"] = $this->getRequestData("moduleIsShow");//是否显示
			$modelInfo["module_thumb"] = "";//模块图标
			$modelInfo["modulelevel"] = "100" ;//$this->getRequestData("txt_modulelevel");//
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new moduleModel();
				$id = $model->add($modelInfo);
				if($id>0)
				{
					$this->sysMsg("新增模块成功",0,array(array("text"=>"返回列表","href"=>$this->_manageDomain."moduleManage/show/"),array("text"=>"继续新增","href"=>$this->_manageDomain."moduleManage/addModule/?groupid=".$moduleGroupID)),true);
				}
			}
		}
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),array("text"=>"模块新增")));
		
		$moduleGroup=new moduleGroupModel();
		$allGroup=$moduleGroup->selectAll();
		
		$this->render("module/addmodule",array("allGroup"=>$allGroup, "moduleGroupID"=>$moduleGroupID, "error"=>$errorStr));
	}
	
	public function editModule()
	{
		$moduleID = $this->getRequestData("id",'GET');
		if(!$moduleID)
		{
			$this->sysMsg("模块ID不正确",1);
		}
		$errorStr="";
		$rule=array(
			"txt_module_name"=>array(
				"formValidator"=>array("tipid"=>"txt_module_name_tip","empty"=>"false","onshow"=>"请输入模块名","onfocus"=>"请输入模块名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块名不能为空")
			),
			"txt_identity_name"=>array(
				"formValidator"=>array("tipid"=>"txt_identity_name_tip","empty"=>"false","onshow"=>"请输入模块唯一标识","onfocus"=>"请输入模块唯一标识","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块唯一标识不能为空")
			),
			"txt_url"=>array(
				"formValidator"=>array("tipid"=>"txt_url_tip","empty"=>"false","onshow"=>"请输入模块访问路径","onfocus"=>"请输入模块访问路径","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"模块访问路径不能为空")
			)
		);
		$this->formValidator->init(array("formID"=>"tbl_base_moduleForm"));
		$this->formValidator->setRules($rule);
		$model=new moduleModel();
		
		if($_POST){
			$modelInfo = array();
			$modelInfo["module_id"] = $moduleID;
			$modelInfo["module_name"] = $this->getRequestData("txt_module_name");//(模块名)
			//$modelInfo["identity_name"] = $this->getRequestData("txt_identity_name");//(模块唯一标识)
			$modelInfo["url"] = $this->getRequestData("txt_url");//(模块访问路径)
			$modelInfo["description"] = $this->getRequestData("txt_description");//(模块描述)
			$modelInfo['module_group_id'] = $this->getRequestData("newModuleGroupID");
			$modelInfo['is_change'] = $this->getRequestData("newModuleGroupID")==$this->getRequestData("oldModuleGroupID")?0:1;
			$modelInfo["module_state"] = $this->getRequestData("moduleState");//模块状态
			$modelInfo["update_time"] = date('Y-m-d H:i:s',strtotime ("now"));//修改日期
			$modelInfo["is_show"] = $this->getRequestData("moduleIsShow");//是否显示
			$modelInfo["module_thumb"] = "";//模块图标

			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model->update($modelInfo);
				header("location:/admin/moduleManage/show/");
				exit();
			}
		}
		else
		{
			$modelInfo = $model->selectById($moduleID);
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
		}
			
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),array("text"=>"模块新增")));
		
		$moduleGroup=new moduleGroupModel();
		$allGroup=$moduleGroup->selectAll();
		
		$this->render("module/editmodule",array("allGroup"=>$allGroup,"modelInfo"=>$modelInfo, "error"=>$errorStr));
	}
	
	function delModule()
	{
		$id = $this->getRequestData("id","GET");
		
		if($id)
		{
			$model=new moduleModel();
			$model->deleteByPkId($id);
		}

		$this->show();
	}
	
	function delgroup()
	{
		$id = $this->getRequestData("id","GET");
		
		if($id)
		{
			$model = new moduleModel();
			$model->deleteByGroup($id);//删除子模块数据
			
			$model=new moduleGroupModel();
			$model->deleteByPkId($id);//删除父模块数据
		}

		$this->show();
	}
	
	function move()
	{    
		//模块排序
		if(!empty($_GET['movenum']))
		{
            $isUp = $this->getRequestData('isup',"GET");//向上为1，向下为0
            $groupID = $this->getRequestData('groupid',"GET");//组id
            $moduleID = $this->getRequestData('moduleid',"GET");//模块id
            $moveNum = $this->getRequestData('movenum',"GET");//移动步数
            $sort = $this->getRequestData('sort',"GET");//当前排序数
            
            if($groupID==0)
            {//移动的是模块组
                $moveInfo=array(
                    'is_up'=>$isUp,
                    'module_id'=>$moduleID,
                    'move_num'=>$moveNum,
                    'sort_num'=>$sort
                    );
				
                $model = new moduleGroupModel();
                $model->move($moveInfo);
            }
            else 
            {//移动是子模块
                $moveInfo=array(
                    'is_up'=>$isUp,
                    'group_id'=>$groupID,
                    'module_id'=>$moduleID,
                    'move_num'=>$moveNum,
                    'sort_num'=>$sort
                    );
                    
				$model = new moduleModel();
                $model->move($moveInfo);
            }
		}
		$this->show();
    }
    
    /**
    * 获取模块列表
    */
	private function getModule()
	{
        $moduleModel = new moduleModel();
		$module=$moduleModel->selectAll();
		
		foreach ($module as $v)
		{
			if (strlen($v['modulelevel']) == 3)
			{
				$v['renderName'] = '' . $v['module_name'];
				$this->tempModuleTree[] = $v;
				$this->getSubModule($module, $v['modulelevel']);
			}
		}
	}
		
	private $tempModuleTree = array();
	//得到模块输出字符串
	private function getSubModule($module, $parentLevel)
	{
		foreach ($module as $v)
		{
			if (substr($v['modulelevel'], 0, strlen($parentLevel)) == $parentLevel && strlen($v['modulelevel']) == strlen($parentLevel) + 3)
			{
				$v['renderName'] = str_repeat("&nbsp;&nbsp;", strlen($parentLevel)) . $v['module_name'];
				$this->tempModuleTree[] = $v;
				$this->getSubModule($module, $v['modulelevel']);
			}
		}
	}
    
	/**
	 * 模块权限列表
	 */
	public function rightLists()
	{
		$moduleID = $this->getRequestData("moduleID","GET");
		$moduleName = $this->getRequestData("moduleName","GET");
			//显示列表
	    if(!$moduleID && empty($this->moduleid))
	    {
	    	$this->sysMsg("模块ID不正确",1);
	    }
	    if(!$moduleName && empty($this->modulename))
	    {
	    	$this->sysMsg("模块名不正确",1);
	    }
	    
	    $module = new moduleRightModel();

	    $param = array(
	    	"moduleID"=>$moduleID,
	    	"moduleName"=>$moduleName,
	    	"allRight"=>$module->selectByModuleId($moduleID)
	    );
	    $this->setMenu(array(array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),array("text"=>"模块权限列表(".$moduleName.")")));
	    $this->render("moduleRight/index",$param);
	}
	
	public function rightAdd(){
		$moduleID = $this->getRequestData("moduleID","GET");
		$moduleName = $this->getRequestData("moduleName","GET");
		
		$errorStr="";
		$rule=array(
			"txt_right_value"=>array(
				"formValidator"=>array("tipid"=>"txt_right_value_tip","empty"=>"false","onshow"=>"请输入权限值","onfocus"=>"请输入权限值","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"权限值不能为空")
			),
			"txt_right_name"=>array(
				"formValidator"=>array("tipid"=>"txt_right_name_tip","empty"=>"false","onshow"=>"请输入权限名","onfocus"=>"请输入权限名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"权限名不能为空")
			),
			"txt_right_ename"=>array(
				"formValidator"=>array("tipid"=>"txt_right_ename_tip","empty"=>"false","onshow"=>"请输入权限英文名","onfocus"=>"请输入权限英文名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"权限英文名不能为空")
			),
		);
		$this->formValidator->init(array("formID"=>"tbl_base_module_rightForm"));
		$this->formValidator->setRules($rule);
		
		$backUrl = $this->_manageDomain."moduleManage/rightLists/?moduleID=".$moduleID."&moduleName=".urlencode($moduleName);
		
		if($_POST){
			$modelInfo = array();
			$modelInfo["module_id"] = $moduleID;
			$modelInfo["right_value"] = $this->getRequestData("txt_right_value");//权限值
			$modelInfo["right_name"] = $this->getRequestData("txt_right_name");//权限名
			$modelInfo["right_ename"] = $this->getRequestData("txt_right_ename");//权限英文名
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new moduleRightModel();
				$id=$model->insert($modelInfo);
				if($id>0)
				{
					$this->sysMsg("新增成功",0,array(array("text"=>"返回列表","href"=>$backUrl),array("text"=>"继续新增","href"=>$this->_manageDomain."moduleManage/rightAdd/?moduleName={$moduleName}&moduleID={$moduleID}")));
				}
			}
		}
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));

		$this->setMenu(array(
							array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),
							array("text"=>"模块权限列表(".$moduleName.")","href"=>$backUrl),
							array("text"=>"权限新增")
							)
						);
						
		$param = array(
	    	"backUrl"=>$backUrl,
	    	"error"=>$errorStr
	    );
	    
		$this->render("moduleRight/add",$param);
	}
	
	public function rightEdit()
	{
		$module_right_id = $this->getRequestData("id","GET");
		$moduleID = $this->getRequestData("moduleID","GET");
		$moduleName = $this->getRequestData("moduleName","GET");
		
		$modelInfo = array();
		$errorStr = "";

		$rule=array(
			"txt_right_value"=>array(
				"formValidator"=>array("tipid"=>"txt_right_value_tip","empty"=>"false","onshow"=>"请输入权限值","onfocus"=>"请输入权限值","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"权限值不能为空")
			),
			"txt_right_name"=>array(
				"formValidator"=>array("tipid"=>"txt_right_name_tip","empty"=>"false","onshow"=>"请输入权限名","onfocus"=>"请输入权限名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"权限名不能为空")
			),
			"txt_right_ename"=>array(
				"formValidator"=>array("tipid"=>"txt_right_ename_tip","empty"=>"false","onshow"=>"请输入权限英文名","onfocus"=>"请输入权限英文名","oncorrect"=>"输入正确"),
				"inputValidator"=>array("min"=>"1","onerror"=>"权限英文名不能为空")
			),
		);
		$this->formValidator->init(array("formID"=>"tbl_base_module_rightForm"));
		$this->formValidator->setRules($rule);
		$backUrl = $this->_manageDomain."moduleManage/rightLists/?moduleID=".$moduleID."&moduleName=".urlencode($moduleName);
		
		$model = new moduleRightModel();
		if($_POST){
			$modelInfo["module_right_id"] = $module_right_id;
			$modelInfo["right_value"] = $this->getRequestData("txt_right_value");//权限值
			$modelInfo["right_name"] = $this->getRequestData("txt_right_name");//权限名
			$modelInfo["right_ename"] = $this->getRequestData("txt_right_ename");//权限英文名
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model->updateByPkId($modelInfo);
				header("location:".$backUrl);
				exit();
			}
		}
		else{
			$modelInfo = $model->selectById($module_right_id);
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
		}
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		
		$this->setMenu(array(
							array("text"=>"模块列表","href"=>$this->_manageDomain."moduleManage/show"),
							array("text"=>"模块权限列表(".$moduleName.")","href"=>$backUrl),
							array("text"=>"权限修改")
							)
						);
						
		$this->render("moduleRight/edit",array("error"=>$errorStr,"modelInfo"=>$modelInfo,"backUrl"=>$backUrl));
	}
	
	public function rightDel()
	{
		$module_right_id = $this->getRequestData("id","GET");
		if($module_right_id>0)
		{
			$model = new moduleRightModel();
			$model->delete($module_right_id);
		}

		$this->rightLists();
	}
}

